# -*- coding: utf_8 -*-
# @Time : 2021/8/19 0019 15:09
# @Author : lyw
# @Remark : 定制接口view
import requests

from mxsoftpy import View, Response

from server.common.IOTManager.Janitor.customize import OenDataServer


class OenData(View):
    """
    中控(一体机)
    """

    def get(self):
        """
        初始考勤机
        @return: str
        """
        param = self.request.GET
        options = param.get("options")
        sn = param.get("SN")
        if options == "all" and sn:
            # 考勤机初始化
            res = ["GET OPTION FROM:%s" % sn,
                   "ATTLOGStamp=None",
                   "ErrorDelay=30",
                   "Delay=300",
                   "TransTimes=00:00;05:00;10:00;15:00;20:00",
                   "TransInterval=1",
                   "TransFlag=110010000000",
                   "TimeZone=8",
                   "Realtime=1"]
            return Response("\n".join(res))
        return Response("ok")

    def post(self):
        """
        接收考勤机数据
        @return:
        """
        headers = self.request.headers
        param = self.request.GET
        door_data = requests.get(
            "http://%s/janitor/public/data/origin.sy?host=%s" % (headers["Host"], self.request.peer_ip))
        door_dict = {}
        # try:
        for door in door_data.json()["data"]["door"]:
            door_dict[door["device_id"]] = door["id"]
        sn = param.get("SN")
        if sn not in door_dict:
            return door_dict
        table_str = param.get("table", "").lower()
        cdata_server = OenDataServer(headers["Host"], sn)

        table = {"attlog": "att_log", "operlog": "oper_log"}

        if hasattr(cdata_server, table.get(table_str, "")):
            res_str = getattr(cdata_server, table.get(table_str, ""))
            # return Response("ok:%s"%res_str(door_dict[sn], "111"))
            return res_str(door_dict[sn], "OPLOG")
        return Response("ok")


class GetRequest(View):
    """
    中控(一体机)
    """

    def get(self):
        """
        考勤机操作数据
        @return: 返回ok 是正常 返回空是异常
        """
        headers = self.request.headers
        param = self.request.GET
        door_data = requests.get(
            "http://%s/janitor/public/data/origin.sy?host=%s" % (headers["Host"], self.request.peer_ip))
        if door_data.status_code != 200:
            return Response("")
        if door_data.json().get("status") != "failed":
            return Response("")

        origin_data = door_data.json()["data"]["data_origin"]
        info = param.get("INFO", "")
        if not info:
            return Response("ok")
        sn = param.get("SN", "")
        info = info.split(",")
        data = {"host": info[4],
                "label": sn,
                "auth_method": ",".join([str(i + 1) for i, value in enumerate(info[9]) if value == "1"]),
                "device_id": sn,
                "origin_id": origin_data["id"],
                "model_id": origin_data["model_id"],
                }
        requests.post("http://%s/janitor/public/add/door.sy" % headers["Host"], data=data)
        return Response("ok")
